#coding UTF-8
FILE_LIST_JS = "list_files_js"

if ARGV[0] == nil
  puts "SYNTAX : ruby minimize.rb outputfile"
  exit
end
outputFileName = ARGV[0]

if !File.exist?(FILE_LIST_JS)
  puts "File : #{FILE_LIST_JS} can't be found."
  exit
end

listeJs = File.open("list_files_js","r")
outputFile = File.new(outputFileName,"w")
listeJs.each_line{|jsName|
  jsName.strip!()
  next if jsName[0] == "#"
  if !File.exist?(jsName)
    puts "Error in #{$FILE_LIST_JS} : #{jsName} can't be found."
    next
  end
  fileJs = File.open(jsName,"r")
  nbLines = 0
  fileJs.each_line{|line_js|
    line = line_js.strip()
    next if /^\s*\/\/.*/.match(line)
    next if line == nil
    next if line.size == 0
    line = line.split("//")[0] if /^.*\s*\/\/[^;]*$/.match(line)
    outputFile.write(line)
    nbLines += 1
  }
  outputFile.puts("")
    
  puts "File #{jsName} parsed with success : #{nbLines} lines"
}
puts "Output file : #{outputFileName}"
listeJs.close()
outputFile.close()